##########################################
# Cross sectional properties of sections #
# 15/02/2012                             #
# (c) jdec@jedrzejdec.eu                 #
# license: GNU GPL v.3 (www.gnu.org)     #
##########################################

"""Calculate cross section properties of different basic shapes.

Nomenclature:
A - area
C - centroid height (y axis)
D - outer diameter
d - inner diameter
b - width
h - height
---------------------------------------------------------
Ix - moment of inertia about x axis
Iy - moment of inertia about y axis
kx - radius of gyration abot x axis
ky - radius of gyration abot y axis
Jz - polar moment of area about z axis through C

REMEMBER to enter data as FLOATS, ex rectangle(2.0,5.0)"""

import math

pi = math.pi

def rectangle(b, h):
    """ Rectangular shape.
    Takes two arguments:
    b - width
    h - height"""
    A = b*h
    Ix = (b*h**3)/12
    Iy = (h*b**3)/12
    Jz = Ix + Iy
    kx = math.sqrt(Ix/A)
    ky = math.sqrt(Iy/A)
    C = (b/2,h/2)
    print 'A =',A
    print 'Ix =',Ix
    print 'Iy =',Iy
    print 'Jz =',Jz
    print 'kx =',kx
    print 'ky =',ky
    print 'C =',C

def circle(D):
    """Circular shape.
    Takes only one diameter"""
    A = (pi*D**2)/4
    Ix = (pi*D**4)/64
    Iy = (pi*D**4)/64
    Jz = (pi*D**4)/32
    kx = math.sqrt(Ix/A)
    ky = math.sqrt(Iy/A)
    C = (0,0)
    print 'A =',A
    print 'Ix =',Ix
    print 'Iy =',Iy
    print 'Jz =',Jz
    print 'kx =',kx
    print 'ky =',ky
    print 'C =',C
    
def hollow_circle(d,D):
    """Hollow cicrle shape.
    Takes two arguments:
    d - inner dia
    D - outer dia"""
    A = 0.25*pi*(D**2-d**2)
    Ix = (pi*D**4)/64
    Iy = (pi*D**4)/64
    Jz = (pi/32)*(D**4-d**4)
    kx = math.sqrt(Ix/A)
    ky = math.sqrt(Iy/A)
    C = (0,0)
    print 'A =',A
    print 'Ix =',Ix
    print 'Iy =',Iy
    print 'Jz =',Jz
    print 'kx =',kx
    print 'ky =',ky
    print 'C =',C
    
def solid_half_circle(D):
    """Half circle shape. Solid.
    Takes only one argument (D - outer dia)"""
    A = (pi*D**2)/8
    Ix = 0.1098*(0.5*D)**4
    Iy = (pi/8)*(0.5*D)**4
    Jz = Ix + Iy
    a = 0.4244*(0.5*D)
    b = 0.5756*(0.5*D)
    kx = math.sqrt(Ix/A)
    ky = math.sqrt(Iy/A)
    C = (0.5*D,a)
    print 'A =',A
    print 'Ix =',Ix
    print 'Iy =',Iy
    print 'Jz =',Jz
    print 'kx =',kx
    print 'ky =',ky
    print 'C =',C

def triangle(b,h):
    """Right triangle shape.
    Takes two arguments:
    b - width
    h - height"""
    A = (b*h)/2
    Ix = (b*h**3)/36
    Iy = (h*b**3)/36
    Jz = Ix + Iy
    kx = math.sqrt(Ix/A)
    ky = math.sqrt(Iy/A)
    C = (b/3,h/3)
    print 'A =',A
    print 'Ix =',Ix
    print 'Iy =',Iy
    print 'Jz =',Jz
    print 'kx =',kx
    print 'ky =',ky
    print 'C =',C

if __name__ == '__main__':
    print '---circle(5.0)---'
    circle(5.0)
    print '---hollow_circle(2.0,5.0)---'
    hollow_circle(2.0,5.0)
    print '---rectangle(10.0,3.5)---'
    rectangle(10.0,3.5)
    print '---solid_half_circle(5.0)---'
    solid_half_circle(5.0)
    print '---triangle(10.0,3.5)---'
    triangle(10.0,3.5)
